<include file="public/header"/>

<body>

<!--main-->
<style type="text/css">
body{ background:#f4f4f4;}
</style>
<div class="porder-payTips" id="timer">

</div>
<script>
    var maxtime = 5 * 60; //
    function CountDown() {
        if (maxtime >= 0) {
            minutes = Math.floor(maxtime / 60);
            seconds = Math.floor(maxtime % 60);
            msg = "请在5分钟内完成支付，距离结束还有" + minutes + "分" + seconds + "秒";
            document.all["timer"].innerHTML = msg;
            --maxtime;
        } else{
            clearInterval(timer);
            layer.msg("订单已被取消",{icon:2,time:2000},function(){ window.location.href = "{:U('Order/index')}"; });
        }
    }
    timer = setInterval("CountDown()", 1000);
</script>

<div class="pPersonal-warp">
  <div class="porder-payPrice">
    支付金额：<span class="c-orange">￥<strong id="goods_price">{$order.goods_price}</strong></span>
  </div>
  <div class="porder-payCash">
    <!--<dl class="clearfix">-->
      <!--<dt>现金券</dt>-->
      <!--<dd><a href="javascript:;" class="use">使用现金券</a></dd>-->
    <!--</dl>-->
    <dl class="clearfix">
      <dt>余额支付</dt>
      <dd>余额￥{$user.balance}
        <div class="check">
          <input type="checkbox" name="balance_pay" id="nan" data-balance="{$user.balance}">
          <label for="nan"></label>
        </div>
      </dd>
    </dl>
    <dl class="clearfix">
      <dt>还需支付</dt>
      <dd style="color: #ff7e23">￥<span class="c-orange" id="pay-price">{$order.goods_price}</span></dd>
    </dl>
  </div>

  <form id="order_info">
    <input type="hidden" name="is_use" value="0" id="is_use">
    <input type="hidden" name="itemid" value="{$order.order_sn}">
  </form>

  <script>
    $(function () {
        $('#nan').click(function () {
            var balance = $('#nan').attr('data-balance'); //余额
            var goods_price = $('#goods_price').html(); //总支付价
            if($("#nan").is(':checked') === true){ //选中的时候
                var price = goods_price - balance;
                price = price.toFixed(2)
                if (price <= 0){
                    price = 0;
                }
                $('#pay-price').html(+price);
                $("#is_use").val(1);
            }else{
                $('#pay-price').html(goods_price);
                $("#is_use").val(0);
            }
        });
    })
  </script>

  <div class="pdata-column-save">
    <a href="javascript:;" class="s-btn" onclick="pay_order()">确认支付</a>
    <a href="{:U('User/center')}" class="s-btn" style="background: #FFB346;margin-top: 10px;">返回个人中心</a>
  </div>
</div>
<!--main End-->

<script>
  function pay_order() {
      $.ajax({
          type : "POST",
          url:"/Home/Payment/pay_order.html",
          data : $('#order_info').serialize(),// 你的formid 搜索表单 序列化提交
          dataType:'json',
          success: function(result){
              if (result.status == 200){
                  //还需要支付，调取微信支付
                  function onBridgeReady(){
                      WeixinJSBridge.invoke(
                          'getBrandWCPayRequest', {
                              "appId":result.result.appId,     //公众号名称，由商户传入
                              "timeStamp":result.result.timeStamp,         //时间戳，自1970年以来的秒数
                              "nonceStr":result.result.nonceStr, //随机串
                              "package":result.result.package,
                              "signType":"MD5",         //微信签名方式：
                              "paySign":result.result.paySign //微信签名
                          },
                          function(res){
                              if(res.err_msg == "get_brand_wcpay_request:ok" ){
                                  location.href = '/home/order/index.html';
                              }
                              if(res.err_msg == "get_brand_wcpay_request:cancel" ){
                                  location.href = '/home/order/index.html';
                              }
                              if(res.err_msg == "get_brand_wcpay_request:fail" ){
                                  location.href = '/home/order/index.html';
                              }
                          });
                  }
                  if (typeof WeixinJSBridge == "undefined"){
                      if( document.addEventListener ){
                          document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                      }else if (document.attachEvent){
                          document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                          document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                      }
                  }else{
                      onBridgeReady();
                  }
              }else{
                  if (result.status == 400){
                      //余额支付成功
                      layer.msg(result.msg,{icon:1,time:2000},function(){if(result.url){ window.location.href = result.url;}});
                  }else{
                      //遇到错误
                      layer.msg(result.msg,{icon:2,time:2000},function(){if(result.url){ window.location.href = result.url;}});
                  }
              }
          }
      });
  }
</script>

</body>
</html>
